package com.example.bookstore.service.impl;

import com.example.bookstore.entity.Notice;
import com.example.bookstore.enums.ResultEnum;
import com.example.bookstore.form.noticeform.InsertNoticeForm;
import com.example.bookstore.form.noticeform.UpdateNoticeForm;
import com.example.bookstore.mapper.NoticeMapper;
import com.example.bookstore.service.NoticeService;
import com.example.bookstore.utils.PageUtil;
import com.example.bookstore.utils.ResultVOUtil;
import com.example.bookstore.utils.TimeUtil;
import com.example.bookstore.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Repository
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    NoticeMapper noticeMapper;

    @Override
    public ResultVO InsertNotice(InsertNoticeForm insertNoticeForm) throws ParseException {
        Notice notice = new Notice();
        notice.setTitle(insertNoticeForm.getTitle());
        notice.setDetails(insertNoticeForm.getDetails());
        notice.setNoticeTime(TimeUtil.getNowDateTime());

        try {
            noticeMapper.InsertNotice(notice);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success(notice);
    }

    @Override
    public ResultVO UpdateNotice(UpdateNoticeForm updateNoticeForm) {
        try {
            noticeMapper.UpdateNotice(updateNoticeForm);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success();
    }

    @Override
    public ResultVO DeleteNotice(Integer noticeId) {
        try{
            noticeMapper.DeleteNotice(noticeId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success();
    }

    @Override
    public ResultVO SelectNoticeById(Integer noticeId) {
        Notice notice = new Notice();
        try {
            notice = noticeMapper.SelectNoticeById(noticeId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success(notice);
    }

    @Override
    public ResultVO SelectNoticeByTitle(String title) {
        List<Notice> noticeList = new ArrayList<>();
        try {
            noticeList = noticeMapper.SelectNoticeByTitle(title);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success(noticeList);
    }

    @Override
    public ResultVO ShowNewNotice() {
        Notice notice = new Notice();
        try {
            notice = noticeMapper.ShowNewNotice();
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success(notice);
    }

    @Override
    public ResultVO ShowAllNotice() {
        List<Notice>  noticeList = new ArrayList<>();
        try{
            noticeList = noticeMapper.ShowAllNotice();
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap = PageUtil.getMap(noticeList);

        return ResultVOUtil.success(hashMap);
    }
}
